#if UNITY_EDITOR
using System;
using System.Collections;
using System.Reflection;
using UnityEditor;
using UnityEngine;

#endif

public static class GizmosUtil
{
    public static bool ShouldRenderGizmos()
    {
#if UNITY_EDITOR
        Assembly assembly = typeof(EditorWindow).Assembly;

        bool drawGizmos = false;
        
        // play mode view
        Type playModeViewType = assembly.GetType("UnityEditor.PlayModeView");
        var playModeViewsField = playModeViewType.GetField("s_PlayModeViews", BindingFlags.NonPublic | BindingFlags.Static);
        var showGizmosField = playModeViewType.GetField("m_ShowGizmos", BindingFlags.NonPublic | BindingFlags.Instance);
        var playModeViews = (IList) playModeViewsField.GetValue(null);

        for (int i = 0; i < playModeViews.Count; i++)
        {
            drawGizmos |= (bool) showGizmosField.GetValue(playModeViews[i]);
            if (drawGizmos)
                break;
        }
        
        if (drawGizmos)
        {
            return drawGizmos;
        }
        
        // scene view
        Type sceneViewType = assembly.GetType("UnityEditor.SceneView");

        var sceneViewsField = sceneViewType.GetField("s_SceneViews", BindingFlags.NonPublic | BindingFlags.Static);
        var sceneViews = (ArrayList) sceneViewsField.GetValue(null);

        for (int i = 0; i < sceneViews.Count; i++)
        {
            drawGizmos |= (sceneViews[i] as SceneView).drawGizmos;
            if (drawGizmos)
                break;
        }

        return drawGizmos;
#else
        return false;
#endif
    }
}